package com.blogsoho.taurus.services.impl;

import com.blogsoho.taurus.dao.mapper.AttachmentMapper;
import com.blogsoho.taurus.services.AttachmentService;
import com.blogsoho.taurus.vm.ResponsePagerWrapper;
import com.blogsoho.taurus.vm.ResponseWrapper;
import com.blogsoho.taurus.vo.AttachmentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by 16978 on 2017/6/17.
 */
@Service
public class AttachmentServiceImpl implements AttachmentService {
    @Autowired
    private AttachmentMapper attachmentMapper;

    @Override
    public ResponseWrapper<Integer> addAttachment(AttachmentVo attachment) {
        ResponseWrapper<Integer> response = new ResponseWrapper<>();
        boolean result = attachmentMapper.addAttachment(attachment) > 0 ? true : false;
        response.setSuccess(result);
        response.setData(attachment.getId());
        return response;
    }

    @Override
    public ResponseWrapper<AttachmentVo> getAttachmentByMD5(String md5) {
        ResponseWrapper<AttachmentVo> response = new ResponseWrapper<>();
        AttachmentVo attachment = attachmentMapper.getAttachmentByMD5(md5);
        if (attachment != null) {
            response.setData(attachment);
        } else {
            response.setSuccess(false);
        }
        return response;
    }

    @Override
    public ResponseWrapper<Integer> addAttachmentMapping(int aid, int cid) {
        ResponseWrapper<Integer> response = new ResponseWrapper<>();
        boolean result = attachmentMapper.addAttachmentMapping(aid, cid) > 0 ? true : false;
        response.setSuccess(result);
        return response;
    }


    @Override
    public ResponsePagerWrapper<List<AttachmentVo>> getAllAttachment(int start, int length) {
        ResponsePagerWrapper<List<AttachmentVo>> response = new ResponsePagerWrapper<>();
        List<AttachmentVo> customers = attachmentMapper.getAllAttachment(start, length);
        int countAttachment = attachmentMapper.getCount();
        response.setSuccess(true);
        response.setData(customers);
        response.setRecordsFiltered(countAttachment);
        response.setRecordsTotal(countAttachment);
        return response;
    }
}
